<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="close"
  >
    <el-checkbox-group v-model="roleIds">
      <!-- 多选的选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 这里准备复选框 -->
    <template #footer>
      <el-button type="primary" size="small" @click="addRoleList">
        确定
      </el-button>
      <el-button size="small" @click="$emit('update:showRoleDialog')">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'Assgin',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 选择的角色
      roleIds: [],
      // 选择角色的id
      userId: null,
      // 获取的角色列表
      list: []
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 确定添加
    async addRoleList () {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:showRoleDialog')
      this.$message.success('分配成功')
    },
    // 调用接口数据回显
    async getRoleListData (id) {
      const res = await getUserDetailById(id)
      this.roleIds = res.roleIds
      this.userId = id
    },
    // 获取列表数据
    async getList () {
      const { rows } = await getRoleList({
        page: 1,
        pagesize: 1000
      })
      this.list = rows
    },
    // 关闭对话框
    close () {
      this.$emit('update:showRoleDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-checkbox__label {
  margin-bottom: 15px;
}
</style>
